package net.pyrogames.jmetest;


import net.pyrogames.deled.importer.DXSImporter;
import net.pyrogames.jme.ExtendedJME2Format;
import net.pyrogames.jme.extension.AttachmentExtension;

import com.jme.app.SimpleGame;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleSystem;

public class HelloAttachmentPoints extends SimpleGame {
	public static void main(String[] args){
		HelloAttachmentPoints app = new HelloAttachmentPoints();
		app.start();
	}
	@Override
	protected void simpleInitGame() {
		//Create the importer
		DXSImporter dxs = new DXSImporter();
		
		//Add attachmentpoint extension
		dxs.addExtension(new AttachmentExtension());
		
		//Create your format
		ExtendedJME2Format format = new ExtendedJME2Format();
		
		//Set importer attributes
		format.setScale(.5f);
		format.setTextureDirectory(HelloDXSImporter.class.getClassLoader().getResource("net/pyrogames/jmetest/data/"));

		//Import the data
		dxs.importModel(HelloDXSImporter.class.getClassLoader().getResource("net/pyrogames/jmetest/data/turret.dxs"), format);

		//Attach to the scene (or use format.save(...) to save to a file)
		rootNode.attachChild(format.getData());
		
		//Set a cull state
		CullState cs = display.getRenderer().createCullState();
		cs.setEnabled(true);
		cs.setCullFace(CullState.Face.Back);
		rootNode.setRenderState(cs);
		rootNode.updateRenderState();
		
		//Show the attachment points
		Node n = new Node("attachmentPoints");
		format.getData().updateWorldVectors(true);
		for(Spatial s : format.getData().getChildren()){
			if(s instanceof Node){
				Box b = new Box("Attp_Point", new Vector3f(0,0,0), 1.0f, 1.0f, 1.0f);
				b.setLocalTranslation(s.getWorldTranslation());
				n.attachChild(b);
				//Create a particle at one of the attachment points
				if(s.getName().equals("attp_particle")){
					((Node)s).attachChild(createFire(s.getLocalTranslation()));
				} 
			}
		}
		format.getData().attachChild(n);
		
		//Recommended to flush the importer and format if you're reusing them
		dxs.flush();
		format.flush();
		//And if you re-use the importer and want to reset extensions
		dxs.flushExtensions();
		
	}
	
	private ParticleSystem createFire(Vector3f loc){
		ParticleSystem particleGeom = ParticleFactory.buildParticles("fire", 300);
		particleGeom.setLocalScale(.15f);
		particleGeom.getLocalTranslation().set(loc.negate());

        particleGeom.setEmissionDirection(new Vector3f(0.0f, 1.0f, 0.0f));
        particleGeom.setMaximumAngle(0.20943952f);
        particleGeom.setMinimumAngle(0);
        particleGeom.getParticleController().setSpeed(1.0f);
        particleGeom.setMinimumLifeTime(1000.0f);
        particleGeom.setMaximumLifeTime(1500.0f);
        particleGeom.setStartSize(40.0f);
        particleGeom.setEndSize(40.0f);
        particleGeom
                .setStartColor(new ColorRGBA(1.0f, 0.312f, 0.121f, 1.0f));
        particleGeom.setEndColor(new ColorRGBA(1.0f, 0.312f, 0.121f, 0.0f));
        particleGeom.getParticleController().setControlFlow(true);
        particleGeom.setReleaseRate(300);
        particleGeom.setReleaseVariance(0.0f);
        particleGeom.setInitialVelocity(0.3f);
        particleGeom.getParticleController().setRepeatType(
                Controller.RT_WRAP);
		
	    BlendState as = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
	    as.setBlendEnabled(true);
	    as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
	    as.setTestEnabled(true);
	    as.setTestFunction(BlendState.TestFunction.GreaterThan);
	    as.setDestinationFunction(BlendState.DestinationFunction.One);

	    TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
	    ts.setTexture(TextureManager.loadTexture(HelloAttachmentPoints.class
	                .getClassLoader().getResource(
	                        "net/pyrogames/jmetest/data/flaresmall.jpg"),
	                Texture.MinificationFilter.BilinearNearestMipMap, Texture.MagnificationFilter.Bilinear));
		
	    ZBufferState zs = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
		particleGeom.setRenderState(zs);
		zs.setWritable(false);
		zs.setEnabled(true);
		particleGeom.setRenderState(zs);
	    particleGeom.setRenderState(ts);
	    particleGeom.setRenderState(as);
	    particleGeom.updateRenderState();
	    particleGeom.warmUp(10);
	     
		return particleGeom;
	}

}
